package br.com.ozsolucoes.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;

/**
 * 
 * @author Felipe de Sousa
 * @since 20/09/2013
 * @version 1.0 - 20/09/2013
 * @implents Serializable Entidade Ticket
 * 
 */

@Entity
@NamedQueries({
		@NamedQuery(name = "Ticket.consultarTodosTickets", query = "select t from Ticket t where t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorId", query = "select t from Ticket t where t.servico.id = :idTicket and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorNumeroTicket", query = "select t from Ticket t where t.numeroTicket = :numeroTicket and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarTodosTicketsCliente", query = "select t from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicao", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoCliente", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoNivelUrgencia", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.nivelUrgencia = :nivelUrgencia and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoNivelUrgenciaCliente", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.nivelUrgencia = :nivelUrgencia and t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoCategoria", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.categoria.categoria = :categoria and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoCategoriaCliente", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.categoria.categoria = :categoria and t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgencia", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaCliente", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaCategoria", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and  t.categoria = :categoria and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaCategoriaCliente", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and  t.categoria = :categoria and t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorCategoria", query = "select t from Ticket t where t.categoria.categoria = :categoria and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.consultarPorCategoriaCliente", query = "select t from Ticket t where t.categoria.categoria = :categoria and t.servico.cliente.id = :idcliente and t.dataTermino is null"),

		@NamedQuery(name = "Ticket.consultarTodosTicketsFechados", query = "select t from Ticket t where t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorIdFechados", query = "select t from Ticket t where t.servico.id = :idTicket and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorNumeroTicketFechados", query = "select t from Ticket t where t.numeroTicket = :numeroTicket and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarTodosTicketsClienteFechados", query = "select t from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoClienteFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoNivelUrgenciaFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.nivelUrgencia = :nivelUrgencia and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoNivelUrgenciaClienteFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.nivelUrgencia = :nivelUrgencia and t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoCategoriaFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.categoria.categoria = :categoria and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorTipoRequisicaoCategoriaClienteFechados", query = "select t from Ticket t where t.tipoRequisicao = :tipoRequisicao and t.categoria.categoria = :categoria and t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaFechados", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaClienteFechados", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaCategoriaFechados", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and  t.categoria = :categoria and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorNivelUrgenciaCategoriaClienteFechados", query = "select t from Ticket t where t.nivelUrgencia = :nivelUrgencia and  t.categoria = :categoria and t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorCategoriaFechados", query = "select t from Ticket t where t.categoria.categoria = :categoria and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.consultarPorCategoriaClienteFechados", query = "select t from Ticket t where t.categoria.categoria = :categoria and t.servico.cliente.id = :idcliente and t.dataTermino is not null"),

		@NamedQuery(name = "Ticket.contarTicketAbertosCliente", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.contarTicketFechadosCliente", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.contarTicketAbertosClientePorServico", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.servico.id = :idServico and t.dataTermino is null"),
		@NamedQuery(name = "Ticket.contarTicketFechadosClientePorServico", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.servico.id = :idServico and t.dataTermino is not null"),
		@NamedQuery(name = "Ticket.contarTicketFechadosClientePorCategoria", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is not null and t.categoria.categoria =:categoria"),
		@NamedQuery(name = "Ticket.contarTicketAbertosClientePorCategoria", query = "select count(t) from Ticket t where t.servico.cliente.id = :idCliente and t.dataTermino is null and t.categoria.categoria =:categoria"),
		@NamedQuery(name = "Ticket.contarTicketClientePorCategoria", query = "select t from Ticket t where t.servico.cliente.id = :idCliente and t.categoria.categoria =:categoria"),
		@NamedQuery(name = "Ticket.consultarTicketDoServico" , query = "select t from Ticket t where t.servico.id = :idServico")
})
public class Ticket implements Serializable {

	private static final long serialVersionUID = -8945169229434384439L;

	@Id
	@GeneratedValue
	private long id;

	private String descricao;

	private String numeroTicket;

	private String nivelUrgencia;

	private String tipoRequisicao;

	private Date dataAbertura;

	private Date dataTermino;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = Servico.class)
	private Servico servico;

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, targetEntity = Documento.class)
	private Documento documento;

	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = Categoria.class)
	private Categoria categoria;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getDescricao() {
		return descricao;
	}

	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}

	public String getNumeroTicket() {
		return numeroTicket;
	}

	public void setNumeroTicket(String numeroTicket) {
		this.numeroTicket = numeroTicket;
	}

	public String getNivelUrgencia() {
		return nivelUrgencia;
	}

	public void setNivelUrgencia(String nivelUrgencia) {
		this.nivelUrgencia = nivelUrgencia;
	}

	public String getTipoRequisicao() {
		return tipoRequisicao;
	}

	public void setTipoRequisicao(String tipoRequisicao) {
		this.tipoRequisicao = tipoRequisicao;
	}

	public Date getDataAbertura() {
		return dataAbertura;
	}

	public void setDataAbertura(Date dataAbertura) {
		this.dataAbertura = dataAbertura;
	}

	public Date getDataTermino() {
		return dataTermino;
	}

	public void setDataTermino(Date dataTermino) {
		this.dataTermino = dataTermino;
	}

	public Servico getServico() {
		return servico;
	}

	public void setServico(Servico servico) {
		this.servico = servico;
	}

	public Documento getDocumento() {
		return documento;
	}

	public void setDocumento(Documento documento) {
		this.documento = documento;
	}

	public Categoria getCategoria() {
		return categoria;
	}

	public void setCategoria(Categoria categoria) {
		this.categoria = categoria;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((categoria == null) ? 0 : categoria.hashCode());
		result = prime * result + ((dataAbertura == null) ? 0 : dataAbertura.hashCode());
		result = prime * result + ((dataTermino == null) ? 0 : dataTermino.hashCode());
		result = prime * result + ((descricao == null) ? 0 : descricao.hashCode());
		result = prime * result + ((documento == null) ? 0 : documento.hashCode());
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((nivelUrgencia == null) ? 0 : nivelUrgencia.hashCode());
		result = prime * result + ((numeroTicket == null) ? 0 : numeroTicket.hashCode());
		result = prime * result + ((servico == null) ? 0 : servico.hashCode());
		result = prime * result + ((tipoRequisicao == null) ? 0 : tipoRequisicao.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Ticket other = (Ticket) obj;
		if (categoria == null) {
			if (other.categoria != null)
				return false;
		} else if (!categoria.equals(other.categoria))
			return false;
		if (dataAbertura == null) {
			if (other.dataAbertura != null)
				return false;
		} else if (!dataAbertura.equals(other.dataAbertura))
			return false;
		if (dataTermino == null) {
			if (other.dataTermino != null)
				return false;
		} else if (!dataTermino.equals(other.dataTermino))
			return false;
		if (descricao == null) {
			if (other.descricao != null)
				return false;
		} else if (!descricao.equals(other.descricao))
			return false;
		if (documento == null) {
			if (other.documento != null)
				return false;
		} else if (!documento.equals(other.documento))
			return false;
		if (id != other.id)
			return false;
		if (nivelUrgencia == null) {
			if (other.nivelUrgencia != null)
				return false;
		} else if (!nivelUrgencia.equals(other.nivelUrgencia))
			return false;
		if (numeroTicket == null) {
			if (other.numeroTicket != null)
				return false;
		} else if (!numeroTicket.equals(other.numeroTicket))
			return false;
		if (servico == null) {
			if (other.servico != null)
				return false;
		} else if (!servico.equals(other.servico))
			return false;
		if (tipoRequisicao == null) {
			if (other.tipoRequisicao != null)
				return false;
		} else if (!tipoRequisicao.equals(other.tipoRequisicao))
			return false;
		return true;
	}

}
